package timing.ukulele.auth.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import timing.ukulele.auth.mapper.RegisteredClientSimpleMapper;
import timing.ukulele.auth.persistent.OAuth2AuthorizationConsentSimple;
import timing.ukulele.auth.persistent.RegisteredClientSimple;

@Service
public class RegisteredClientSimpleService extends ServiceImpl<RegisteredClientSimpleMapper, RegisteredClientSimple> {
    public IPage<RegisteredClientSimple> getPage(RegisteredClientSimple filter, int current, int pageSize) {
        Page<RegisteredClientSimple> page = new Page<>(current, pageSize);
        LambdaQueryWrapper<RegisteredClientSimple> query = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(filter.getClientName())) {
            query.like(RegisteredClientSimple::getClientName, filter.getClientName());
        }
        query.orderByDesc(RegisteredClientSimple::getClientId);
        Page<RegisteredClientSimple> result = this.getBaseMapper().selectPage(page, query);
        return result;
    }

    public IPage<OAuth2AuthorizationConsentSimple> getUserConsentPage(String username, int current, int pageSize) {
        Page<OAuth2AuthorizationConsentSimple> page = new Page<>(current, pageSize);
        IPage<OAuth2AuthorizationConsentSimple> iPage = this.getBaseMapper().userConsentList(page, username);
        return iPage;
    }


    public IPage<OAuth2AuthorizationConsentSimple> getClientConsentUserPage(String clientId, int current, int pageSize) {
        Page<OAuth2AuthorizationConsentSimple> page = new Page<>(current, pageSize);
        IPage<OAuth2AuthorizationConsentSimple> iPage = this.getBaseMapper().clientConsentUserList(page, clientId);
        return iPage;
    }

    public Boolean deleteUserConsent(String username, String clientId) {
        int i = this.getBaseMapper().deleteUserConsent(username, clientId);
        return i >= 0;
    }
}
